import { request } from "@/common/request";
import config from "@/common/config";
/**
 * 刷新token
 * @returns
 */
export function refreshToken(data: any) {
  return request({
    url: `/auth/refresh`,
    method: "post",
    data,
  });
}

/**
 * 登录
 */
export function postLogin(data: any, header: any = {}) {
  const pam = {
    grant_type: "password",
    scope: "all",
    type: "account",
    tenantId: config.getTenantId(),
    ...data,
  };
  let url = ObjectUrl("/kingon-auth/oauth/token", pam);
  // console.log(JSON.stringify(loginData))
  return request({
    url,
    method: "post",
    data: {},
    header: {
      noVerify: true,
      ...header,
    },
  });
}

/**
 * 退出登录
 */
export function oauthLogout() {
  return request({
    url: "/kingon-auth/oauth/logout",
    method: "get",
    header: {
      isReLaunch: false,
    },
  });
}
/**
 * 切换部门/角色/刷新token
 */
export function changeDept(data = {}, header = {}) {
  let url = ObjectUrl("/kingon-auth/oauth/token", data);
  // console.log('changeDept >>',loginData)
  return request({
    url,
    method: "post",
    data: {},
    header: {
      noVerify: true,
      ...header,
    },
  });
}

/**
 * 演示平台验证码登录
 */
export function bindPhoneByIp(data: any) {
  return request({
    url: "/kingon-system/userLoginLog/bindPhoneByIp",
    method: "get",
    data,
  });
}

/**
 * 修改密码
 */
export function postUpdatePassword(data: any) {
  return request({
    url: ObjectUrl("/kingon-system/update-password", data),
    method: "post",
    data,
  });
}

/**
 * 重设密码(忘记密码)
 */
export function resetPassword(data: any) {
  return request({
    url: ObjectUrl("/kingon-sojourn-core/mobile-user-gate/reset-password", data),
    method: "post",
    data,
  });
}

/**
 * 第三方注册
 */
export function guestRegister(data: any) {
  return request({
    url: "/kingon-system/register-guest",
    method: "post",
    data,
  });
}

/**
 * 隐私政策 服务协议
 */
export function systemclauseAgreementdetail(data = {}) {
  return request({
    url: "/kingon-system/clauseAgreement/detail",
    method: "get",
    data,
  });
}

/**
 * 获取当前账号信息(简单)
 */
export function getInfo() {
  return request({
    url: "/kingon-system/info",
    method: "get",
  });
}
/**
 * 获取当前账号信息（详细）
 */
export function getInfoDetails() {
  return request({
    url: "/kingon-system/getInfo",
    method: "get",
  });
}

/**
 *  我的 > 个人信息 > 更新账户资料 （系统接口-取自PC管理平台-账号管理）
 */
export function updateAccount(data = {}) {
  return request({
    url: "/kingon-system/update-info",
    method: "post",
    data,
  });
}

// 查询账号信息 （系统接口-取自PC管理平台-账号管理）
export function getAccountById(data = {}) {
  return request({
    url: "/kingon-system/detail",
    method: "post",
    data,
  });
}

export function ObjectUrl(url: string, data: any) {
  Object.keys(data).forEach((v, index) => {
    if (index == 0) {
      url += "?" + v + "=" + data[v];
    } else {
      url += "&" + v + "=" + data[v];
    }
  });
  return url;
}
